<?php

class Index_Model extends Model {

    public function __construct() {
        parent::__construct();
    }

    public function getData() {
        $data = array();
        if (isset($_GET['action']) && $_GET['action'] = 'logout') {
            Session::destroy();
            header('Location: ./index.php');
        }

        if (Session::get('loggedIn') === true) {
            $data['loggedIn'] = true;
            $data['userName'] = Session::get('userName');
        } else {
            if (isset($_POST['action']) && $_POST['action'] = 'login') {
                $sth = $this->db->prepare("SELECT * FROM user WHERE name = :login AND pwd = :password");
                $sth->execute(array(
                    ':login' => $_POST['login'],
                    ':password' => $_POST['password']
                ));

                $res = $sth->fetch();
                $count = $sth->rowCount();
                if ($count > 0) {

                    // login
                    Session::set('loggedIn', true);
                    Session::set('userName', $res['name']);
                    $data['loggedIn'] = true;
                    $data['userName'] = $res['name'];
                    // nicht nötig nur wegen laden jassi msg, da jessi prüfung in helper session ist noch nicht da
                    header('Location: ./index.php');
                } else {
                    $data['loggedIn'] = false;
                }
            }

        }
        return $data;
    }

}